package com.beiding.dbrequest.part3;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author 丁常磊
 * @date 2021/12/23 12:21
 */

public abstract class LastElementSubscriber<T> implements Observer<T> {

    private AtomicReference<T> last = new AtomicReference<>();

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(T t) {
        //cas赋值
        last.compareAndSet(last.get(), t);
    }

    @Override
    public void onError(Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void onComplete() {
        gotLast(last.get());
    }

    /**
     * 获取到最后一个值时
     */
    public abstract void gotLast(T t);

}
